<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;

/* @var $this yii\web\View */
/* @var $model common\models\News */
/* @var $form yii\widgets\ActiveForm */
?>
<?=Html::jsFile('/static/common/js/ajaxfileupload.js')?>
<style>
    .cke_contents {
        height: 400px!important;
    }
</style>
<div class="news-form">

    <?php $form = ActiveForm::begin(); ?>

    <table class="table" border="0" width="100%">
    <?/*= $form->field($model, 'category_id')->textInput() */?>
        <tr>
            <td width="50%"><?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?></td>
            <td><?= $form->field($model, 'status')->dropDownList(\common\helpers\TStringHelper::getStatusText()) ?></td>
        </tr>
        <tr>
            <td>
                <?= $form->field($model, 'category_id')->dropDownList($category, ['prompt' => Yii::t('common', 'select...')]) ?>
            </td>
            <td><?= $form->field($model, 'order')->textInput() ?></td>
        </tr>
        <tr>
            <td><?= $form->field($model, 'litpic')->textInput(['id' => 'litpic']) ?>
                <!--<div id="litpic-div" class="<?/*=$model->litpic?'':'hidden'*/?>">
                    <img src="<?/*=$model->litpic*/?>" class="img-rounded" />
                </div>-->
                <input type="file" name="Filedata" class="file_upload_attach" id="upload_child_attach" data-id="attach">
            </td>
            <td><?= $form->field($model, 'tag')->textInput(['maxlength' => true])->hint('标签之间以英文逗号","分隔，前台相关新闻以此为关联', ['style' => 'color: #777']) ?></td>
        </tr>
        <tr>
            <td colspan="2">
                <?= $form->field($model, 'desc')->textarea() ?>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <?= $form->field($model, 'content')->widget(\dosamigos\ckeditor\CKEditor::className(),[
                    'options' => ['row' => 10],
                    'preset' => 'basic',
                    'clientOptions' => [
                        'filebrowserUploadUrl' => \yii\helpers\Url::toRoute(['common/url', 'event' => 0]),
                    ]
                ]) ?>
            </td>
        </tr>
        <tr>
            <td><?= $form->field($model, 'author')->textInput(['maxlength' => true]) ?></td>
            <td><?= $form->field($model, 'comefrom')->textInput(['maxlength' => true]) ?></td>
        </tr>
        <tr>
            <td><?= $form->field($model, 'views')->textInput(['maxlength' => true]) ?></td>
            <td><?= $form->field($model, 'shares')->textInput(['maxlength' => true]) ?></td>
        </tr>
    <?/*= $form->field($model, 'remark')->textInput(['maxlength' => true]) */?>
    </table>
    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? Yii::t('common', 'create') : Yii::t('common', 'update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>
<script>
    $(function () {
        //此处用了change事件，当选择好图片打开，关闭窗口时触发此事件
        $("#upload_child_attach").change(function (e) {
            var val = e.target.value;
            var point = val.lastIndexOf(".");
            var type = val.substr(point).toLowerCase();
            if (type != ".jpg" && type != ".jpeg" && type != ".png") {
                alert('系统目前只支持.jpg、.jpeg、.png文件格式');
                return false;
            }
            $.ajaxFileUpload({
                //处理图片的脚本路径
                url: '<?=\yii\helpers\Url::toRoute(['common/upload-thumb', 'event' => 1, 'dir' => 'news', 'thumb' => '250,160'])?>',
                type: 'post',       //提交的方式
                secureuri: false,   //是否启用安全提交
                fileElementId: 'upload_child_attach',     //file控件ID
                dataType: 'json',  //服务器返回的数据类型
                success: function (data, status) {  //提交成功后自动执行的处理函数
                    if (data.error) {
                        alert(data.msg);
                        return false;
                    } else {
                        $("input[id='litpic']").val(data.fileUrl);
                        //$("#litpic-div").removeClass('hidden');
                        //$("#litpic-div").find('img').attr('src', data.fileUrl);
                    }
                },
                error: function (data, status, e) {   //提交失败自动执行的处理函数
                    alert(e);
                }
            })
        })
    })
</script>